package com.youlin.config;

import io.netty.util.concurrent.DefaultThreadFactory;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import java.util.concurrent.*;

@Configuration
public class ThreadPoolExecutorConfig {

    @Bean
    public ThreadPoolExecutor threadPoolExecutor () {
        Integer corePoolSize = Runtime.getRuntime().availableProcessors() * 2;
        Integer maxPoolSize = Runtime.getRuntime().availableProcessors() * 10;
        Integer keepAliveTime = 10;
        TimeUnit timeUnit = TimeUnit.SECONDS;
        BlockingQueue<Runnable> workQueue = new LinkedBlockingDeque<>(1000000);
        ThreadFactory threadFactory = new  DefaultThreadFactory("MyConfigPool");
        RejectedExecutionHandler rejectedExecutionHandler = new ThreadPoolExecutor.CallerRunsPolicy();
        ThreadPoolExecutor threadPoolExecutor = new ThreadPoolExecutor(corePoolSize, maxPoolSize,
                keepAliveTime, timeUnit, workQueue, threadFactory, rejectedExecutionHandler);
        threadPoolExecutor.prestartAllCoreThreads(); // 预启动线程池中的所有核心线程
        return threadPoolExecutor;
    }

}
